package com.qc31.monitorlib.ui.carmonitor

import android.view.View
import autodispose2.autoDispose
import com.qc31.baselib.base.BaseActivity
import com.qc31.baselib.utils.MLog
import com.qc31.comlib.Keys
import com.qc31.comlib.UrlUtil
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.databinding.ActivityPlatformVideoBinding
import com.qc31.monitorlib.ui.video.CustomVideoManager
import java.util.*

/**
 *@Author Jasper
 *@Time   2020/10/15
 *@Desc   平台视频
 */
class PlatFormVideoActivity :
    BaseActivity<ActivityPlatformVideoBinding>(ActivityPlatformVideoBinding::inflate) {
    private var uuid: String = ""
    override fun initView() {
        val carId = intent.getStringExtra(Keys.INTENT_CAR_ID)
        val title = intent.getStringExtra(Keys.INTENT_TITLE)
        val fileName = intent.getStringExtra(Keys.INTENT_DATA)
        val duration = intent.getLongExtra(Keys.INTENT_OTHER, 0)
        binding.pfVideo.titleTextView.text = title

        uuid = UUID.randomUUID().toString()
        val url = "${UrlUtil.mVideoHost}/httpflv.splay.$uuid.$carId.$fileName"

        binding.pfVideo.setUpLazy(url, false, null, null, title)
        binding.pfVideo.durationT = duration
        binding.pfVideo.titleTextView.visibility = View.VISIBLE
        binding.pfVideo.backButton.visibility = View.VISIBLE
    }

    override fun setListener() {
        binding.pfVideo.backButton.setOnClickListener {
            CustomVideoManager.instance.releaseAllVideos()
            finish()
        }
        binding.pfVideo.startPlay.setOnClickListener {
            binding.pfVideo.startPlayLogic()
        }
    }

    override fun onBackPressed() {
        binding.pfVideo.setVideoAllCallBack(null)
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        binding.pfVideo.onVideoPause()
        CustomVideoManager.instance.onPause()
    }

    override fun onResume() {
        super.onResume()
        binding.pfVideo.onVideoResume()
        CustomVideoManager.instance.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.pfVideo.release()
        closeVideo()
        CustomVideoManager.instance.releaseAllVideos()
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    }

    private fun closeVideo() {
        ServiceHelper.getRepository()
            .getVideoState("${UrlUtil.mVideoHost}/httpflv.close.$uuid")
            .autoDispose(scopeProvider).subscribe({
                MLog.e("关闭播放成功=====$it")
            }, {
                MLog.e("关闭播放失败=====$it")
            })
    }

}